package com.zlc.common.core.sys.service.impl;

import com.zlc.common.common.exception.BusinessException;
import com.zlc.common.common.service.BaseServiceImpl;
import com.zlc.common.core.sys.mapper.MayanPlatParameterMapper;
import com.zlc.common.core.sys.model.entity.MayanPlatParameter;
import com.zlc.common.core.sys.model.vo.MayanPlatParameterVo;
import com.zlc.common.core.sys.service.MayanPlatParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author zl
 * @date 2025-08-20 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MayanPlatParameterServiceImpl extends BaseServiceImpl<MayanPlatParameterMapper, MayanPlatParameter, MayanPlatParameterVo> implements MayanPlatParameterService {

    @Autowired
    private MayanPlatParameterMapper mayanPlatParameterMapper;

    @Override
    public String getString(String key) {
        MayanPlatParameterVo parameterVo = getByKey(key);
        if (ObjectUtils.isEmpty(parameterVo)) {
            throw new BusinessException("缺少系统参数:" + key);
        }
        return parameterVo.getDefaultValue();
    }

    @Override
    public Boolean getBoolean(String key) {
        String bool = getString(key);
        if (bool.equals("true")) {
            return true;
        }
        return false;
    }

    @Override
    public Integer getInteger(String key) {
        String integer = getString(key);
        try {
            return Integer.parseInt(integer);
        } catch (Exception e) {
            throw new BusinessException("系统参数:" + key + "不是整数");
        }
    }

    private MayanPlatParameterVo getByKey(String key) {
        List<MayanPlatParameterVo> parameterVos = queryVoList(wrapper().eq(MayanPlatParameter::getParameterKey, key));
        return !parameterVos.isEmpty() ? parameterVos.get(0) : null;
    }
}
